import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
// import Home from '../views/Home.vue'
// import Mine from '../views/Mine.vue'
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/Home'
    },
    {
      path: '/Home',
      name: 'Home',
      component: () => import('../views/Home.vue')
    },
    {
      path: '/Mine',
      name: 'Mine',
      component: () => import('../views/Mine.vue'),
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/CategoryList',
      name: 'CategoryList',
      component: () => import('../views/CategoryList.vue')

    },
    {
      path: '/Cart',
      name: 'Cart',
      component: () => import('../views/Cart.vue'),
      // meta: {
      //   requiresAuth: true
      // }
    },
    {
      path: '/MyLogin',
      name: 'MyLogin',
      component: () => import('../components/MyLogin.vue')
    },
    {
      path: '/MyDetail',
      name: 'MyDetail',
      component: () => import('../components/MyDetail.vue'),
      props: true
    },
    {
      path: '/MyNavList/:proid',
      component: () => import('../components/MyNavList.vue'),
      props: true
    }

  ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    if (!sessionStorage.getItem("username")) {
      next({ path: "/MyLogin", query: { path: to.path } });
    } else {
      next();
    }
  } else {
    next();
  }
})


export default router
